package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.bean.RankingBean;
import com.yogee.topseller.utils.AppUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/7.
 */

public class RankingRvAdapter extends RecyclerView.Adapter {
    private Context context;
    private int TYPE_ITEM = 0, TYPE_FOOT = 1;
    private int rankType = 0;
    private List<RankingBean.ProductListBean> data = new ArrayList<>();

    public RankingRvAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<RankingBean.ProductListBean> data, int rankType) {
        this.data = data;
        this.rankType = rankType;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == data.size()) {
            return TYPE_FOOT;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOT) {
            View view = LayoutInflater.from(context).inflate(R.layout.foot_ranking, parent, false);
            return new FootHolder(view);
        } else { //item_ranking_new
            View view = LayoutInflater.from(context).inflate(R.layout.item_is_list, parent, false);
            return new ItemHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemHolder) {
            final RankingBean.ProductListBean bean = data.get(position);
            ((ItemHolder) holder).nameTv.setText(bean.getName());
            if(AppUtil.isExamine(context))
                ((ItemHolder) holder).yongBiTv.setText(bean.getCommission());
            else
                ((ItemHolder) holder).yongBiTv.setText("登录可见");
            if (!TextUtils.isEmpty(bean.getRecruitmentProportion())) {
                int pro = Integer.valueOf(Arrays.asList(bean.getRecruitmentProportion().split("\\.")).get(0));
                ((ItemHolder) holder).yongPb.setProgress(pro);
            }
            ((ItemHolder) holder).yongProTv.setText(bean.getRecruitmentProportion() + "%");
            ((ItemHolder) holder).monthTv.setText(bean.getInvestmentTerm());
            String labels = "";
//            List<String> list = Arrays.asList(bean.getProductLable().split(","));

//            for (int i = 0; i < list.size(); i++) {
//                labels += " " + list.get(i);
//            }
            ((ItemHolder) holder).detailsTv.setText(bean.getProType());
            ((ItemHolder) holder).syTv.setText(bean.getExpectReturn().substring(0,bean.getExpectReturn().length()-1));
            ((ItemHolder) holder).qtMoneyTv.setText(bean.getStartAmount());

            // 预热和图片
            if(bean.getImgstatus().equals("1")){
                ((ItemHolder) holder).img_tu.setVisibility(View.VISIBLE);
            }else{
                ((ItemHolder) holder).img_tu.setVisibility(View.GONE);
            }

            if(bean.getStatus().equals("预热中"))
                ((ItemHolder) holder).img_yu.setVisibility(View.VISIBLE);
            else{
                ((ItemHolder) holder).img_yu.setVisibility(View.GONE);
            }
            if(bean.getStatus().equals("售罄")){
                ((ItemHolder) holder).img_sell_out.setVisibility(View.VISIBLE);
            }else
                ((ItemHolder) holder).img_sell_out.setVisibility(View.GONE);


            /*
            if (rankType == 2) {
//                ((ItemHolder) holder).yongIv.setImageResource(R.mipmap.ranking_commission_black);
                ((ItemHolder) holder).yongBiTv.setTextColor(Color.parseColor("#4a4a4a"));

                ((ItemHolder) holder).syTv.setText(bean.getExpectReturn());
                ((ItemHolder) holder).syTv.setTextColor(context.getResources().getColor(R.color.colorStyle));
//                ((ItemHolder) holder).syDetailsTv.setText("预期收益");
                ((ItemHolder) holder).syDetailsTv.setTextColor(context.getResources().getColor(R.color.colorStyle));

                ((ItemHolder) holder).qtMoneyTv.setText(bean.getStartAmount());
//                ((ItemHolder) holder).qtMoneyDetailsTv.setText("起投金额");
            } else {
//                ((ItemHolder) holder).yongIv.setImageResource(R.mipmap.product_commission_red);
                ((ItemHolder) holder).yongBiTv.setTextColor(context.getResources().getColor(R.color.colorStyle));

                ((ItemHolder) holder).syTv.setText(bean.getStartAmount());
//                ((ItemHolder) holder).syDetailsTv.setText("起投金额");
                ((ItemHolder) holder).qtMoneyTv.setText(bean.getExpectReturn());
//                ((ItemHolder) holder).qtMoneyDetailsTv.setText("预期收益");

                ((ItemHolder) holder).syTv.setTextColor(Color.parseColor("#cccccc"));
                ((ItemHolder) holder).syDetailsTv.setTextColor(Color.parseColor("#4a4a4a"));
            }
            */

            ((ItemHolder) holder).itemRankingLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, ProductDetailsActivity.class).putExtra("id", bean.getId()).putExtra("isFast", "0"));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_list_name_tv)
        TextView nameTv;
        @BindView(R.id.item_list_mj_tv)
        TextView mjTv;
        @BindView(R.id.item_list_details_tv)
        TextView detailsTv;
        @BindView(R.id.item_list_sy_tv)
        TextView syTv;
//        @BindView(R.id.item_ranking_sy_details_tv)
//        TextView syDetailsTv;
        @BindView(R.id.item_list_qt_money_tv)
        TextView qtMoneyTv;
//        @BindView(R.id.item_ranking_qt_money_details_tv)
//        TextView qtMoneyDetailsTv;
        @BindView(R.id.item_list_month_tv)
        TextView monthTv;
//        @BindView(R.id.item_ranking_yong_iv)
//        ImageView yongIv;
        @BindView(R.id.item_list_yong_bi_tv)
        TextView yongBiTv;
        @BindView(R.id.item_list_yong_pro_tv)
        TextView yongProTv;
        @BindView(R.id.item_list_yong_pb)
        ProgressBar yongPb;
        @BindView(R.id.item_list_ll)
        LinearLayout itemRankingLl;
        @BindView(R.id.img_yu)
        ImageView img_yu;
        @BindView(R.id.img_tu)
        ImageView img_tu;
        @BindView(R.id.img_sell_out)
        ImageView img_sell_out;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootHolder extends RecyclerView.ViewHolder {

        public FootHolder(View itemView) {
            super(itemView);
        }
    }
}
